/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.martinkrasa.golftourweb.site;

import com.martinkrasa.golftourweb.site.entity.Tournament;
import com.martinkrasa.golftourweb.site.entity.TournamentResult;
import com.martinkrasa.golftourweb.site.entity.TournamentType;
import java.io.IOException;
import java.util.List;
import javax.inject.Inject;
import org.springframework.stereotype.Service;

/**
 *
 * @author martin.krasa
 */
@Service
public class DefaultTournamentService implements TournamentService {
    
    @Inject TournamentRepository tournamentRepository;
    @Inject TournamentResultRepository tournamentResultRepository;
    @Inject LoadingService loadingService;
    
    @Override
    public Tournament getTournament(long id) {
        return this.tournamentRepository.getTournament(id);
    }
    
    @Override
    public List<TournamentResult> getTournamentResults(long id) {
        return this.tournamentResultRepository.getTournamentResults(id);
    }

    @Override
    public void addTournament(Tournament tournamentToAdd, List<TournamentResult> tournamentResultsToAdd) {
        
        this.tournamentRepository.addTournament(tournamentToAdd);
        this.tournamentResultRepository.addTournamentResults(tournamentToAdd.getId(), tournamentResultsToAdd);
    }

    @Override
    public Tournament loadTournament(String[] bruttoUrlStrings, String[] nettoUrlStrings) {
        
        List<TournamentResult> result;
        Tournament tournament = new Tournament();
        
        try {
            result = this.loadingService.loadTournament(bruttoUrlStrings, nettoUrlStrings);
            
            tournament.setId(0);
            tournament.setSeasonId(0);
            tournament.setWebsiteId("");                                        // TODO
            tournament.setName("Test222");                                      // TODO
            tournament.setType(0);                                              // TODO
            
            this.addTournament(tournament, result);
        
        } catch (IOException | LoadingService.LoadingException ex) {
            ex.getStackTrace();
        }    
        
        return tournament;
    }

}
